package thema2project;

import java.util.List;
import java.util.Iterator;

/**
 * A simple model of a hunter. Hunters age, move, shoot bears, foxes, rabbits,
 * and leaves.
 * 
 */
public class Hunter implements Actor {
	// The animal's field.
	private Field field;
	// The animal's position in the field.
	private Location location;
	private boolean alive;

	/**
	 * Create a hunter. A hunter can be created as a new born (age zero and not
	 * hungry) or with a random age
	 * 
	 * @param randomAge
	 *            If true, the hunter will have random age and hunger level.
	 * @param field
	 *            The field currently occupied.
	 * @param location
	 *            The location within the field.
	 */
	public Hunter(boolean alive, Field field, Location location) {
		this.alive = alive;
		this.field = field;
		setLocation(location);
	}

	private void setLocation(Location newLocation) {
		if (location != null) {
			field.clear(location);
		}
		location = newLocation;
		field.place(this, newLocation);

	}

	/**
	 * This is what the hunter does most of the time: it hunts for rabbits and
	 * foxes. *
	 * 
	 * @param field
	 *            The field currently occupied.
	 * @param newActors
	 *            . A list to add newly born hunters to.
	 */
	public void act(List<Actor> newActor) {

		// Move towards a place to hunt.
		Location location = getLocation();
		Location newLocation = hunt(location);
		if (newLocation == null) {
			// No food found - try to move to a free location.
			newLocation = getField().freeAdjacentLocation(location);
		}
		// See if it was possible to move.
		if (newLocation != null) {
			setLocation(newLocation);
		}
	}

	private Field getField() {
		return field;
	}

	private Location getLocation() {
		return location;
	}

	/**
	 * Tell the hunter to look for animals adjacent to its current location.
	 * Only the first live animal is hunted.
	 * 
	 * @param location
	 *            Where in the field it is located.
	 * @return Where animal was found, or null if it wasn't.
	 */
	public Location hunt(Location location) {
		Field field = getField();
		List<Location> adjacent = field.adjacentLocations(getLocation());
		Iterator<Location> it = adjacent.iterator();
		while (it.hasNext()) {
			Location where = it.next();
			Object animal = field.getObjectAt(where);
			if (animal instanceof Bear) {
				Bear bear = (Bear) animal;
				if (bear.isAlive()) {
					bear.setDead();
					// Remove the dead bear from the field.
					return where;
				}
			}
			if (animal instanceof Fox) {
				Fox fox = (Fox) animal;
				if (fox.isAlive()) {
					fox.setDead();
					// Remove the dead fox from the field.
					return where;
				}
			}
			if (animal instanceof Rabbit) {
				Rabbit rabbit = (Rabbit) animal;
				if (rabbit.isAlive()) {
					rabbit.setDead();
					// Remove the dead rabbit from the field.
					return where;
				}
			}
		}
		return null;
	}

	public void setAlive(boolean isAlive) {
		this.alive = isAlive;
	}

	public boolean isAlive() {
		return alive;
	}
}
